package khleuven.mondzorg.app;

import khleuven.mondzorg.Logger;
import khleuven.mondzorg.R;
import khleuven.mondzorg.app.SimpleGestureFilter.SwipeGestureListener;
import khleuven.mondzorg.domain.Illustrations;
import khleuven.mondzorg.domain.Module;
import khleuven.mondzorg.domain.Test;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class TestActivity extends ActivityWithIllustrations implements
		SwipeGestureListener {

	private Module module;
	private int sequence = 1;
	private Test test;

	private SimpleGestureFilter detector;

	private TextView tvseq;
	private TextView tvquestion;
	private RadioGroup answerbtns;
	private RadioButton answ1btn, answ2btn, answ3btn;

	private int[][] answerpositions = { { 0, 1, 2, 0 }, { 0, 2, 1, 0 },
			{ 1, 0, 2, 1 }, { 1, 2, 0, 2 }, { 2, 0, 1, 1 }, { 2, 1, 0, 2 } };
	private int shuffleidx;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		fetchIntentExtras();

		tvseq = (TextView) findViewById(R.id.txt_sequence);
		tvquestion = (TextView) findViewById(R.id.txt_question);
		answerbtns = (RadioGroup) findViewById(R.id.radgroup_answers);
		answ1btn = (RadioButton) findViewById(R.id.radbtn1);
		answ2btn = (RadioButton) findViewById(R.id.radbtn2);
		answ3btn = (RadioButton) findViewById(R.id.radbtn3);

		setTitle();
		setTest();

		detector = new SimpleGestureFilter(this, this);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent me) {
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me);
	}

	private void fetchIntentExtras() {
		Logger.logApp("aangekomen in fetchIntentExtras Learn");
		Bundle b = getIntent().getExtras();
		if (b != null) {
			Logger.logApp("aangekomen in fetchIntentExtras - bundle niet null");
			module = new Module(b.getString(ModulesActivity.MODULE_NAME));
			Logger.logApp("aangekomen in fetchIntentExtras - bundle niet null module: "
					+ module.getName());
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void setTitle() {
		TextView tvmodule = (TextView) findViewById(R.id.txt_module);
		tvmodule.setText(module.getName());
	}

	private boolean setTest() {
		boolean found = false;
		Logger.logApp("sequence: " + sequence);
		resetAnswerButtons();
		test = dentalcare.getPreferredTest(module, sequence);

		if (test != null) {
			Logger.logApp("test gevonden");
			String lesson = dentalcare.getTestQuestion(test);
			String[] answers = dentalcare.getTestAnswers(test);
			shuffleAnswers();
			setTextViewHTML(tvquestion, lesson);
			tvquestion.setMovementMethod(LinkMovementMethod.getInstance());
			tvseq.setText(getString(R.string.question) + " " + sequence);
			answ1btn.setText(answers[answerpositions[shuffleidx][0]]);
			answ2btn.setText(answers[answerpositions[shuffleidx][1]]);
			answ3btn.setText(answers[answerpositions[shuffleidx][2]]);
			found = true;
		} else {
			Logger.logApp("test niet gevonden");
			tvseq.setText(getString(R.string.questionsup));
			tvquestion.setText(getString(R.string.questionscomplete));

		}
		illustrations = new Illustrations();
		setIllustration();
		return found;
	}

	private void resetAnswerButtons() {
		answ1btn.setText("");
		answ2btn.setText("");
		answ3btn.setText("");
		resetAnswerButtonsColor();
		answ1btn.setChecked(false);
		answ2btn.setChecked(false);
		answ3btn.setChecked(false);
	}

	private void resetAnswerButtonsColor() {
		answ1btn.setTextColor(getResources().getColor(R.color.lichtblauw));
		answ2btn.setTextColor(getResources().getColor(R.color.lichtblauw));
		answ3btn.setTextColor(getResources().getColor(R.color.lichtblauw));
	}

	private void shuffleAnswers() {
		shuffleidx = (int) (Math.random() * 6.0);
	}

	protected void setIllustration() {
		super.setIllustration();
	}

	protected void makeLinkClickable(SpannableStringBuilder strBuilder,
			final URLSpan span) {
		ClickableSpan clickable = new ClickableSpan() {
			public void onClick(View view) {
				String defString = span.getURL();
				Logger.logApp("NU MOETEN WE NAAR DEF: " + defString);
				Intent intent = new Intent(TestActivity.this,
						DefinitionsActivity.class);
				intent.putExtra(DefinitionsActivity.DEF_KEY, defString);
				startActivity(intent);
			}
		};
		super.makeLinkClickable(strBuilder, span, clickable);
	}

	@Override
	public void onSwipe(int direction) {
		if (direction == SimpleGestureFilter.SWIPE_RIGHT) {
			Logger.logApp("swipe right --> next");
			if (sequence > 1) {
				sequence--;
			}
		} else {
			Logger.logApp("swipe left --> previous");
			if (test != null) {
				sequence++;
			}
		}
		setTest();
	}

	public void clickedAnswer(View view) {
		resetAnswerButtonsColor();
		int selectedAnswer = 0;
		int selectedBtnID = answerbtns.getCheckedRadioButtonId();
		if (selectedBtnID == answ2btn.getId()) {
			selectedAnswer = 1;
		} else if (selectedBtnID == answ3btn.getId()) {
			selectedAnswer = 2;
		}

		int correctAnsweridx = answerpositions[shuffleidx][3];
		Logger.logApp("clicked!" + selectedAnswer + " == " + correctAnsweridx+ "???");
		if (selectedAnswer == correctAnsweridx) {
			illustrations = dentalcare.getTestIllustrations(test);
			((RadioButton) answerbtns.getChildAt(selectedAnswer))
					.setTextColor(getResources().getColor(R.color.juist));

		} else {
			illustrations = new Illustrations();
			((RadioButton) answerbtns.getChildAt(selectedAnswer))
					.setTextColor(getResources().getColor(R.color.fout));
		}
		setIllustration();
	}

}
